package com.cmf.myproject.service.security.realm;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;

import com.cmf.myproject.service.security.dto.LoginUserInfo;

import com.cmf.myproject.service.security.authc.MallUsernamePasswordToken;

import lombok.extern.slf4j.Slf4j;


/**
 * Created by h.p on 2018/12/4.
 */
@Slf4j
public class MerchantUserRealmImpl extends AuthorizingRealm {

    private String supportedLoginType;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

        Subject subject = SecurityUtils.getSubject();
        Object user = subject.getPrincipal();
//        authorizationInfo.addRole(user.getRole());
//        authorizationInfo.addStringPermissions(merPers);
        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        log.debug("merchant user realm authentication:" + authenticationToken.toString());
        MallUsernamePasswordToken token = (MallUsernamePasswordToken)authenticationToken;
//        MerUser merUser = merUserRepository.findByPhone(token.getUsername());
//        if (Objects.isNull(merUser)) {
//            throw new UnknownAccountException("登录失败，无此账号");
//        }
        /* 检查商户状态 */
//        Merchant merchant = merchantRepository.findByMerchantId(merUser.getMerchantId());
//        if (Objects.isNull(merchant)) {
//            throw new UnknownAccountException("登录失败，商户不存在");
//        }
//        if (!MerStatusEnum.ENABLE.getCode().equals(merchant.getMstatus().getCode())) {
//            throw new DisabledAccountException("登录失败，商户"+merchant.getMstatus().getMessage());
//        }
//        if (merchant.getIsLocked()) {
//            throw new DisabledAccountException("登录失败，商户已锁定");
//        }
//
//        if (merUser.getIsLocked()) {
//            String msg = "";
//            if (MerchantRoleEnum.manager.name().equals(merUser.getRole().name())) {
//                msg = "登录失败，账号已锁定";
//            } else {
//                msg = "登录失败，账号已禁用";
//            }
//            throw new DisabledAccountException(msg);
//        }
//
//        LoginUserInfo userInfo = new LoginUserInfo();
//        userInfo.setId(merUser.getId().toString());
//        userInfo.setUserName(merUser.getPhone());
//        userInfo.setNickName(merUser.getRealName());
//        userInfo.setType(token.getLoginType());
//        userInfo.setRole(merUser.getRole().name());
//        userInfo.setMerchantId(merUser.getMerchantId());

        LoginUserInfo userInfo = new LoginUserInfo();
        userInfo.setType(token.getLoginType());
        userInfo.setUserName("admin");
        userInfo.setNickName("商户账号");
        AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
        		userInfo,
                "240be518fabd2724ddb6f04eeb1da5967448d7e831c08c8fa822809f74c720a9",
                getName());
        return authenticationInfo;
    }

    @Override
    public boolean supports(AuthenticationToken token){
        if (token instanceof MallUsernamePasswordToken) {
            MallUsernamePasswordToken usernamePasswordToken = (MallUsernamePasswordToken) token;
            return getSupportedLoginType().equals(usernamePasswordToken.getLoginType());
        }
        return false;
    }

    public String getSupportedLoginType() {
        return supportedLoginType;
    }

    public void setSupportedLoginType(String supportedLoginType) {
        this.supportedLoginType = supportedLoginType;
    }
}
